// pages/user/user.js
import { throttle} from '../../../utils/util.js';
import CommonService from '../../../service/CommonService';
import WalletService from '../../../service/WalletService';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.commonService = new CommonService();
    this.walletService = new WalletService();
    this.setData({
      user: app.globalData.user
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    const _ = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '递名片',
      path: '/packageOrder/pages/order/order-detail',
      imageUrl: app.globalData.user.cardImg,
      success: function (res) {
        console.log(res)
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          const { cardScore } = app.globalData.setting;
          _.commonService.active(3).then(function (res) {
            if (res.status == 0) {
              wx.showModal({
                title: '递名片投递成功',
                content: `你获得${cardScore}枚救兵勋章`,
                showCancel: false,
                confirmText: '知道了',
                confirmColor: '#0f9bc1'
              })
            }
          })
        }
      },
    }
  },
  toCreateOrder: function(){
    wx.navigateTo({
      url: '/packageOrder/pages/order/create-order'
    })
  },
  toHome: throttle(function (e) {
    console.log(e);
    wx.reLaunch({
      url: '/pages/home/home'
    })
  }, 1000),
  onTypeTap: throttle(function (e) {
    const user = app.globalData.user
    console.log(e.detail.type)
    switch(e.detail.type){
      case 'setting':
        wx.navigateTo({
          url: '../setting/setting'
        })
        break;
      case 'wallet':
        wx.navigateTo({
          url: '../wallet/wallet'
        })
        break;
      case 'auth':{
        const user = app.globalData.user;
        if (user.userType == 1) {
          wx.navigateTo({
            url: '../auth/business-detail'
          })
        } else if (user.userType == 2) {
          wx.navigateTo({
            url: '../auth/factory-detail'
          })
        }
        break;
      }
      case 'favorite':{
        const user = app.globalData.user;
        let url="";
        if (user.userType == 1) {
          url = '/packageUser/pages/favorite/favorite-factory';
        } else if (user.userType == 2) {
          url = '/packageUser/pages/favorite/favorite-order';
        }
        wx.navigateTo({ url })
        break;
      }
      case 'order':
        wx.navigateTo({
          url: '../order/order-list'
        })
        break;
      case 'message':
        wx.navigateTo({
          url: '/packageUser/pages/message/message'
        })
        break;
    }
  }, 1000),
  /**发红包 */
  doEnvelopSpilt: function (e) {
    const _ = this;
    throttle(function () {
      const {user} = _.data
      _.walletService.factoryHasEnvelop(user.id).then(res=>{
        if(res.status == 0){
          if(!res.data){
            wx.navigateTo({
              url: `/packageUser/pages/envelop/envelop`
            })
          }else{
            wx.showToast({
              title: '红包未领完',
            })
          }
        }else{
          wx.showToast({
            title: '请求出错',
          })
        }
      })
    }, 1000)();
  },
  onShare: function () {
    const _ = this;
    throttle(function () {
      wx.showShareMenu({
        withShareTicket: true,
        success: function () {
          console.log('aaaa')
        }
      })
    },1000)();
  },
})